package fr.castorflex.CshopFlex.Views;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import fr.castorflex.CshopFlex.R;
import fr.castorflex.CshopFlex.Model.Item;
import fr.castorflex.CshopFlex.Utilities.ImageLoader;
import fr.castorflex.CshopFlex.Utilities.JSONOperations;

public class SearchProductsAdapter extends BaseAdapter{
    private Activity activity;
    private ArrayList<Item> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public SearchProductsAdapter(){}
    
    public SearchProductsAdapter(Activity a, ArrayList<Item> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());

        
        
    }

    public int getCount() {
    	
        return data==null? 0 : data.size();
    }

    public Item getItem(int position) {
        return data.get(position);
    } 	

    public long getItemId(int position) {
        return position;
    }
    
    public static class ViewHolder{
        public TextView price;
        public TextView title;
        public ImageView image;
        public ProgressBar progressBar;
    }
    
    

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        
        if(convertView==null){
            vi = inflater.inflate(R.layout.displayitem, null);
            holder=new ViewHolder();
            holder.title=(TextView)vi.findViewById(R.id.textViewItemTitle);
            holder.price=(TextView)vi.findViewById(R.id.textViewItemPrice);
            holder.image=(ImageView)vi.findViewById(R.id.imageView1);
            holder.progressBar = (ProgressBar) vi.findViewById(R.id.progressBarSearchImage);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        
        Item i = data.get(position);
        holder.title.setText(i.getNom());
        
        char currency = i.getCurrency();
        StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append((int)i.getPrix());
		stringBuilder.append(" ");
		stringBuilder.append(currency);
		holder.price.setText(stringBuilder.toString());
		
        holder.image.setTag(i.getLinkImage());
        
        imageLoader.DisplayImage(i.getLinkImage(), activity, holder.image, holder.progressBar);
        
        
        return vi;
    }
    
    public void loadMoreProducts() throws ClientProtocolException, IOException, JSONException
    {
    	data.addAll(JSONOperations.getNextItems());
    }

}
